package edu.zhuoxun.blogserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.zhuoxun.blogserver.entity.Note;
import edu.zhuoxun.blogserver.entity.Page;
import edu.zhuoxun.blogserver.entity.Result;
import edu.zhuoxun.blogserver.map.NoteMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class NoteController {
    @Autowired
    public NoteMapper noteMapper;

    @GetMapping("api/note/page")
    @ResponseBody
    @RequiresPermissions("note:view")
    public Result<Page<Note>> getNotePage(int page, int size) {
        QueryWrapper<Note> wrapper = new QueryWrapper<Note>().orderBy( true,false, "create_date");
        Page<Note> notePage = new Page<>(wrapper, noteMapper, page, size);
        Result<Page<Note>> result = new Result<>();
        result.setMessage("OK");
        result.setCode(200);
        result.setData(notePage);
        return result;
    }
    @PostMapping("api/note/save")
    @ResponseBody
    @RequiresPermissions(value = {"note:add", "note:update"}, logical = Logical.OR)
    public Result<Void> saveNote(@RequestBody Note note) {
        Result<Void> result = new Result<>();
        Subject subject = SecurityUtils.getSubject();
        if (note.getId() != null) {
            if (subject.isPermitted("note:update")) {
                noteMapper.updateById(note);
                result.setCode(200);
                result.setMessage("更新成功");
            } else {
                result.setCode(403);
                result.setMessage("没有修改笔记的权限");
            }

        } else {
            if (subject.isPermitted("note:add")) {
                note.setCreateDate(new Date());
                noteMapper.insert(note);
                result.setCode(200);
                result.setMessage("新建成功");
            } else {
                result.setCode(403);
                result.setMessage("没有新建笔记的权限");
            }

        }

        return result;
    }

    @GetMapping("/api/note/findById")
    @ResponseBody
    @RequiresPermissions("note:view")
    public Result<Note> findNoteById(int id) {
        Note note = noteMapper.selectById(id);
        Result<Note> result = new Result<>();
        if (note != null) {
            result.setCode(200);
            result.setMessage("OK");
            result.setData(note);
        } else {
            result.setCode(500);
            result.setMessage("未找到笔记 id: " + id);
        }
        return result;
    }
    @GetMapping("/api/note/delete")
    @ResponseBody
    @RequiresPermissions("note:delete")
    public Result<Void> deleteById(int id) {
        int count = noteMapper.deleteById(id);
        Result<Void> result = new Result<>();
        if (count == 1) {
            result.setCode(200);
            result.setMessage("OK");
        } else {
            result.setCode(500);
            result.setMessage("id:" + id + " 不存在");
        }
        return result;
    }
    @GetMapping("api/note/search")
    @ResponseBody()
    @RequiresPermissions("note:view")
    public Result<Page<Note>> searchNote(String keyword, int page, int size) {
        QueryWrapper<Note> wrapper = new QueryWrapper<Note>().like("title", keyword).or(noteQueryWrapper -> noteQueryWrapper.like("content_md", keyword));
        Page<Note> notePage = new Page<>(wrapper, noteMapper, page, size);
        Result<Page<Note>> result = new Result<>();
        result.setCode(200);
        result.setMessage("OK");
        result.setData(notePage);
        return result;
    }
}
